import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.RowFilter;
import javax.swing.SpringLayout;
import javax.swing.SwingConstants;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.TableRowSorter;

public class CurrentClientsPanel extends JPanel {
    private boolean DEBUG = false;
    private JTextField filterText;
    private JTable table;
        private TableRowSorter<CurrentClientTable> sorter;
        private JButton openPatientFileButton;

        
        public CurrentClientsPanel() {
                
                this.setBackground(Color.WHITE);
                
                //Create a table with a sorter.
        CurrentClientTable myTable = new  CurrentClientTable();
                sorter = new TableRowSorter<CurrentClientTable>(myTable);
                table = new JTable(myTable);
                table.setRowSorter(sorter);
                table.setPreferredScrollableViewportSize(new Dimension(400, 70));
                table.setFillsViewportHeight(true);
        table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        //When selection changes, outputs status bar for current table location
        table.getSelectionModel().addListSelectionListener(
                new ListSelectionListener() {
                    public void valueChanged(ListSelectionEvent event) {
                        int viewRow = table.getSelectedRow();
                        if (viewRow < 0) {
                            //Selection got filtered away.
                            System.out.println("");
                        } else {
                            int modelRow = 
                                table.convertRowIndexToModel(viewRow);
                            System.out.println(
                                String.format("Selected Row in view: %d. " +
                                    "Selected Row in model: %d.", 
                                    viewRow, modelRow));
                        }
                    }
                }
        );
        
               
      //Create a separate form for 
      //                - filterText search bar - filters by first name currently, needs to filter last also
      //                - statusText in console
      //                - openPatientFile button
        JPanel form = new JPanel(new GridLayout(3,2));
        form.setBackground(Color.WHITE);
        
        // search/filter bar
        JLabel l1 = new JLabel("Search By First Name", SwingConstants.TRAILING);
                form.add(l1);
        filterText = new JTextField();
        filterText.setColumns(10);
            //Whenever filterText changes, invoke newFilter.
                filterText.getDocument().addDocumentListener(
                        new DocumentListener() {
                            public void changedUpdate(DocumentEvent e) {
                                newFilter();
                            }
                            public void insertUpdate(DocumentEvent e) {
                                newFilter();
                            }
                            public void removeUpdate(DocumentEvent e) {
                                newFilter();
                            }
                        });
        
                l1.setLabelFor(filterText);
        form.add(filterText);
              
               
        // skip a cell
        JPanel blank1 = new JPanel();
        blank1.setBackground(Color.WHITE);
        form.add(blank1);
        
        // button 
        openPatientFileButton = new JButton("Open Patient File");
        form.add(openPatientFileButton);
        
        // status bar
        JLabel l2 = new JLabel("Table status in console", SwingConstants.TRAILING);
        form.add(l2);
        
        
      //add the table to a scrollpane so we can add to the CurrentPatientsPanel JPanel
        JScrollPane scrollPane = new JScrollPane(table);
        this.add(form);           //add search bar forms
        this.add(scrollPane); //add table
                        
        }
        
        
         /** 
     * Update the row filter regular expression from the expression in
     * the text box.
     */
    private void newFilter() {
        RowFilter<CurrentClientTable, Object> rf = null;
        //If current expression doesn't parse, don't update.
        try {
            rf = RowFilter.regexFilter(filterText.getText(), 0);
        } catch (java.util.regex.PatternSyntaxException e) {
            return;
        }
        sorter.setRowFilter(rf);
    }
        
        
}
